﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraScheduler;
using System.Threading.Tasks;
using DexERP.Services;
using DevExpress.XtraEditors;

namespace DexERP
{
    public partial class FormMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //barUser.Caption = DexGlobal.User.UserName;
            //.Caption = DexGlobal.User.CompanyName;
            //barDatabase.Caption = DexGlobal.Database.caption;
        }

        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormLoad loadform = new FormLoad();
            loadform.Owner = this;
            loadform.Show();
            DataTable datas = new DataTable();
            string msg = "success";
            Task.Run(() =>
            {
                using (EpicorService epicor = new EpicorService(DexGlobal.Database.host, DexGlobal.Database.uid, DexGlobal.Database.pwd, DexGlobal.Database.dbname))
                {                    
                    datas = epicor.dbHelper.Query("SELECT PartNum,PartDescription,IUM,TypeCode,ClassID,NonStock FROM Erp.Part");
                    //datas = Mapper.Map<List<DTO.Part>>(items);
                }
                this.BeginInvoke(new Action(() =>
                {
                    gridControlM.DataSource = datas;
                    loadform.Hide();
                    if (msg != "success")
                        XtraMessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }));
            });

        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();            
        }

        private void barButtonCfg_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraFormCfg form = new XtraFormCfg();
            form.Show();
        }
    }
}
